package cn.com.blackboard.DataModule4B2.BBDataUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import blackboard.data.ValidationException;
import blackboard.data.user.User;
import blackboard.persist.KeyNotFoundException;
import blackboard.persist.MaxUsersLicenseLimitException;
import blackboard.persist.PersistenceException;
import blackboard.persist.user.UserDbLoader;
import blackboard.persist.user.UserDbPersister;
import blackboard.platform.LicenseUtil;
import cm.com.blackboard.DataModule4B2.customizeConfigUtils.DataSourceKeyUtil;
import cm.com.blackboard.DataModule4B2.customizeConfigUtils.ExtentionAttributeUtil;
import cn.com.blackboard.DataModule4B2.model.StudentInfo;
import cn.com.blackboard.DataModule4B2.model.TeacherInfo;
import cn.com.blackboard.DataModule4B2.util.BatchResultModel;
import cn.com.blackboard.DataModule4B2.util.BatchResultModel.Status;

public class UserUtil {
	
	private static final Logger logger = LoggerFactory.getLogger(UserUtil.class);
	
	private static UserDbLoader userDbLoader = null;
	private static UserDbPersister userDbPersister = null;
	static {
		try {
			userDbLoader = UserDbLoader.Default.getInstance();
			userDbPersister = UserDbPersister.Default.getInstance();
		} catch (PersistenceException e) {
			logger.info("error in UserUtil init UserDbLoader/UserDbPersister"); 
		}
	}
	
	public static Status importTeacher(TeacherInfo teacherInfo){
		User tempu = new User();
		tempu.setGivenName(teacherInfo.getFirstName());
		String departmentName = DepartmentUtil.getDepartmentNameById(teacherInfo.getDepartmentId());
		tempu.setFamilyName(departmentName);
		tempu.setPassword(LicenseUtil.getHashValue(teacherInfo.getPassword()));
		tempu.setEmailAddress(teacherInfo.getEmail());
		tempu.setUserName(teacherInfo.getUserId());
		if(DepartmentUtil.isPortal()){
			String departmentId = DepartmentUtil.appendSilkPortalRoleSuffix(teacherInfo.getDepartmentId());
			tempu.setPortalRoleId(DepartmentUtil.getPortalIdByDepartmentId(departmentId));
		}
		if(ExtentionAttributeUtil.hasExtention(tempu, teacherInfo)){
			ExtentionAttributeUtil.setExtentionAttribute(tempu, teacherInfo);
		}
		DataSourceKeyUtil.setDataSrouceKey(tempu, teacherInfo);
		try {
			userDbPersister.persist(tempu);
			return Status.ImportSuccess;
		} catch (MaxUsersLicenseLimitException e) {
			logger.info("UserUtil importTeacher User count is over limited, please check increase user license"); 
			return Status.ImportFailed;
		} catch (PersistenceException e) {
			logger.info("UserUtil importTeacher User persistenceException"); 
			return Status.ImportFailed;
		} catch (ValidationException e) {
			logger.info("UserUtil importTeacher User "+tempu.getStudentId()+":"+tempu.getUserName()+" is invalid, please check datasource"); 
			return Status.ImportFailed;
		}				
	}
	
	public static Status importStudent(StudentInfo studentInfo){
		User tempu = new User();
		tempu.setGivenName(studentInfo.getFirstName());
		String departmentName = DepartmentUtil.getDepartmentNameById(studentInfo.getDepartmentId());
		tempu.setFamilyName(departmentName);
		tempu.setPassword(LicenseUtil.getHashValue(studentInfo.getPassword()));
		tempu.setEmailAddress(studentInfo.getEmail());
		tempu.setUserName(studentInfo.getUserId());
		if(DepartmentUtil.isPortal()){
			String departmentId = studentInfo.getDepartmentId();
			tempu.setPortalRoleId(DepartmentUtil.getPortalIdByDepartmentId(departmentId));
		}
		if(ExtentionAttributeUtil.hasExtention(tempu, studentInfo)){
			ExtentionAttributeUtil.setExtentionAttribute(tempu, studentInfo);
		}
		DataSourceKeyUtil.setDataSrouceKey(tempu, studentInfo);
		try {
			userDbPersister.persist(tempu);
			return Status.ImportSuccess;
		} catch (MaxUsersLicenseLimitException e) {
			logger.info("UserUtil importStudent User count is over limited, please check increase user license"); 
			return Status.ImportFailed;
		} catch (PersistenceException e) {
			logger.info("UserUtil importStudent User persistenceException");
			e.printStackTrace();
			return Status.ImportFailed;
		} catch (ValidationException e) {
			logger.info("UserUtil importStudent User "+tempu.getStudentId()+":"+tempu.getUserName()+" is invalid, please check datasource"); 
			return Status.ImportFailed;
		}				
	}
	
	public static Status updateStudent(StudentInfo studentInfo){
		try {
			User tempu = userDbLoader.loadByBatchUid(studentInfo.getUserId());
			tempu.setGivenName(studentInfo.getFirstName());
			String departmentName = DepartmentUtil.getDepartmentNameById(studentInfo.getDepartmentId());
			tempu.setFamilyName(departmentName);
			tempu.setPassword(LicenseUtil.getHashValue(studentInfo.getPassword()));
			tempu.setEmailAddress(studentInfo.getEmail());
			tempu.setUserName(studentInfo.getUserId());
			if(DepartmentUtil.isPortal()){
				String departmentId = studentInfo.getDepartmentId();
				tempu.setPortalRoleId(DepartmentUtil.getPortalIdByDepartmentId(departmentId));
			}
			userDbPersister.persist(tempu);
			return Status.UpdateSuccess;
		} catch (KeyNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (PersistenceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ValidationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return Status.UpdateFailed;
	}
	
	public static Status updateTeacher(TeacherInfo teacherInfo){
		try {
			User tempu = userDbLoader.loadByBatchUid(teacherInfo.getUserId());
			tempu.setGivenName(teacherInfo.getFirstName());
			String departmentName = DepartmentUtil.getDepartmentNameById(teacherInfo.getDepartmentId());
			tempu.setFamilyName(departmentName);
			tempu.setPassword(LicenseUtil.getHashValue(teacherInfo.getPassword()));
			tempu.setEmailAddress(teacherInfo.getEmail());
			tempu.setUserName(teacherInfo.getUserId());
			if(DepartmentUtil.isPortal()){
				String departmentId = teacherInfo.getDepartmentId();
				tempu.setPortalRoleId(DepartmentUtil.getPortalIdByDepartmentId(departmentId));
			}
			userDbPersister.persist(tempu);
			return Status.UpdateSuccess;
		} catch (KeyNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (PersistenceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ValidationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return Status.UpdateFailed;
	}

}
